package cn.woochen.comlib.util.ext

import android.annotation.SuppressLint
import android.view.View
import com.jakewharton.rxbinding4.view.clicks
import cn.woochen.comlib.util.OnSingleClickListener
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

fun View.visible(){
    if (this.visibility != View.VISIBLE) this.visibility = View.VISIBLE
}

fun View.gone(){
    if (this.visibility != View.GONE) this.visibility = View.GONE
}

fun View.invisible(){
    if (this.visibility != View.INVISIBLE) this.visibility = View.INVISIBLE
}

@SuppressLint("CheckResult")
fun View.setOnSingleClickListener(timeSecond:Long = 1,singleEvent: (View)->Any?){
    this.clicks()
        .subscribeOn(AndroidSchedulers.mainThread())
        .subscribe {
            if (tag == null || (System.currentTimeMillis() - (tag as Long))> timeSecond*1000L ){
                singleEvent.invoke(this)
                tag = System.currentTimeMillis()
            }
        }
}

fun View.setOnSingleClickListener(singleEvent: OnSingleClickListener?, timeSecond:Long = 1){
    this.clicks()
        .subscribeOn(AndroidSchedulers.mainThread())
        .subscribe {
            if (tag == null || (System.currentTimeMillis() - (tag as Long))> timeSecond*1000L ){
                singleEvent?.onSingleClick(this)
                tag = System.currentTimeMillis()
            }
        }
}
